package dataS.recusion;

public class Triangle {
    static int []ans;

    public static void main(String[] args) {
        //求第n项三角数        1,3,6,10
        int n=10000;
        ans=new int [n+1];


//        System.out.println(test1(n));
//        System.out.println(test3(10000));
        System.out.println(test4(4));
    }

    //有备忘录的写法
    public static int test1(int n){

        if(n==1)
            return ans[1]=1;
        if(ans[n]!=0)
            return ans[n];

        ans[n]=test1(n-1)+n;
        return ans[n];
    }
    //直接递归
    public static int test2(int n){
        if(n==1)
            return 1;
        return test2(n-1)+n;
    }
    //自下而上
    public static int test3(int n){
        int []b=new int[n+1];
        b[1]=1;
        for (int i = 2; i < b.length; i++) {
            b[i]=b[i-1]+i;
        }
        return b[n];
    }
    //自下而上，不占用空间
    public static int test4(int n){
        int count=0;
        while(n>0){
            count=count+n;
            n--;
        }
        return count;
    }



}
